package com.example.strategy;

import com.example.Discount;
import com.example.fruit.Fruit;

import java.math.BigDecimal;
import java.util.List;

/**
 * 打折策略
 */
public class StrategyB implements Strategy {
    @Override
    public BigDecimal getMoney(List<Fruit> fruitList, List<Discount> discountList) {
        BigDecimal all = new BigDecimal(0);
        for (Fruit fruit : fruitList) {
            Boolean isOnSale = false;
            for (Discount discount : discountList) {
                if(fruit.getName().equals(discount.getName())){
                    all = all.add(fruit.getSum().multiply(discount.getDiscount()));
                    break;
                }
                isOnSale = true;
            }
            if(isOnSale){
                all = all.add(fruit.getSum());
            }

        }
        return all.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
